home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / c / sas_c_emacs_b3.lha / slashquote / slashquote.c next >
C/C++ Source or Header  |  1994-03-20  |  5KB  |  206 lines

  1. /*
  2.  *  FILE
  3.  *    slashquote.c
  4.  *
  5.  *  DESCRIPTION
  6.  *    A one function ARexx function library.
  7.  *
  8.  *  AUTHOR
  9.  *    Anders Lindgren, d91ali@csd.uu.se
  10.  *
  11.  *  STATUS
  12.  *    slashquote is free software; you can redistribute it and/or modify
  13.  *    it under the terms of the GNU General Public License as published 
  14.  *    by the Free Software Foundation; either version 1, or (at your 
  15.  *    option) any later version.
  16.  *
  17.  *    GNU Emacs is distributed in the hope that it will be useful,
  18.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *    GNU General Public License for more details.
  21.  *
  22.  *    You should have received a copy of the GNU General Public
  23.  *    License along with GNU Emacs; see the file COPYING.  If not,
  24.  *    write to the Free Software Foundation, 675 Mass Ave, Cambridge,
  25.  *    MA 02139, USA.
  26.  */
  27.  
  28. /* Arguments:
  29.  * Up to 15 arguments may be passed in the rm_Args[] array of the parameter
  30.  * block.  Arguments are ALWAYS passed as argstrings and can generally be
  31.  * treated like string pointers in a 'C' program.  The called function may
  32.  * need to convert the strings to numeric values if arithmetic operations
  33.  * are to be performed.  A NULL value in the argument slot means that the
  34.  * argument was omitted from the function call, as in MyFunc(1,,3).
  35.  *
  36.  * The total number of arguments (including the defaulted ones) is available
  37.  * in the low-order byte of the action code field rm_Action.
  38.  * Note that REXX supports function calls with varying numbers of arguments,
  39.  * and that the called function can always determine how many were actually
  40.  * passed.
  41.  *
  42.  * Error reporting:
  43.  * The function must return an integer error code and a result string if no
  44.  * errors were detected.  Errors are considered to be ARexx internal error
  45.  * codes, so the function should make use of these values as appropriate.
  46.  * A code of 0 is interpreted to mean that everything worked.
  47.  *
  48.  * Result strings:
  49.  * The result string must be returned as an argstring, a pointer to the
  50.  * string buffer of an RexxArg structure.  Argstrings can be created by a
  51.  * call to the ARexx Systems library function CreateArgstring().
  52.  * N.B. Never allocate a result string if the error code is non-zero!
  53.  */
  54.  
  55. #include <exec/types.h>
  56.  
  57. #include <exec/memory.h>
  58.  
  59. #include <rexx/storage.h>
  60. #include <rexx/rxslib.h>
  61. #include <rexx/errors.h>
  62.  
  63. #include <proto/exec.h>
  64. #include <proto/rexxsyslib.h>
  65.  
  66. #include <string.h>
  67. #include <dos.h>
  68.  
  69.  
  70. /*
  71.  *  Forward references.
  72.  */
  73.  
  74. extern long slashquote (struct RexxMsg *, UBYTE * *);
  75.  
  76.  
  77. /*
  78.  *  Global data.
  79.  */
  80.  
  81. struct Library * RexxSysBase;
  82.  
  83.  
  84. /*
  85.  *  FUNCTION
  86.  *    __UserLibInit
  87.  *
  88.  *  DESCRIPTION
  89.  *    Open the rexx system library.
  90.  */
  91.  
  92. int __saveds
  93. __UserLibInit(void)
  94. {
  95.     if (RexxSysBase = OpenLibrary("rexxsyslib.library", 0)) {
  96.     return(0);
  97.     }
  98.     return(1);
  99. }
  100.  
  101.  
  102. /*
  103.  *  FUNCTION
  104.  *    __UserLibCleanup
  105.  *
  106.  *  DESCRIPTION
  107.  *    Close the Rexx system library
  108.  */
  109.  
  110. void __saveds
  111. __UserLibCleanup(void)
  112. {
  113.     if (RexxSysBase) {
  114.     CloseLibrary(RexxSysBase);
  115.     }
  116. }
  117.  
  118.  
  119. /*
  120.  *  FUNCTION
  121.  *    LIBRexxEntry
  122.  *
  123.  *  DESCRIPTION
  124.  *    The library entry point.
  125.  *    Check if this library contains the desired function, otherwise
  126.  *    return a errorcode 1.
  127.  */
  128.  
  129. long __asm __saveds
  130. LIBRexxEntry(register __a0 struct RexxMsg * rmptr)
  131. {
  132.     long        retcode = 1L;    /* Fail by default */
  133.     UBYTE *     retval  = NULL;
  134.  
  135.  
  136.     if (strcmp("SLASHQUOTE", rmptr->rm_Args[0]) == 0) {
  137.     retcode = slashquote(rmptr, & retval);
  138.     } 
  139.     /* Set the return values */
  140.     __builtin_putreg(REG_A0, (long)retval);    /* Return String    */
  141.     return(retcode);
  142. }
  143.  
  144.  
  145. /*
  146.  *  FUNCTION
  147.  *    slashquote
  148.  *
  149.  *  DESCRIPTION
  150.  *    Manipulates a string in the following way:
  151.  *      1) Put quotes around it.
  152.  *      2) Escape all quotes and escapecharacters in the string. The
  153.  *         escapecharacter is the 2:nd arg, or '\' if none is supplied.
  154.  */
  155.  
  156. long 
  157. slashquote(struct RexxMsg * rmptr, UBYTE * * retvalp)
  158. {
  159.     register char    ch;
  160.          char    qch;    /* Character to take care of. */
  161.     register UBYTE * dest;
  162.     register UBYTE * src;
  163.     register long    sindex = 0;
  164.     register long    dindex = 0;
  165.     register int     len;
  166.  
  167.     /* Check the number of arguments */
  168.     if ( (rmptr->rm_Action & 0xFF) == 1 ) {
  169.     qch = '\\';    /* Default, quote backslash */ 
  170.     }
  171.     else if ( (rmptr->rm_Action & 0xFF) == 2 ) {
  172.     qch = rmptr->rm_Args[2][0];
  173.     }
  174.     else {
  175.     return(ERR10_017);
  176.     }
  177.  
  178.     len = LengthArgstring(rmptr->rm_Args[1]);
  179.  
  180.     if (dest = AllocMem((len<<1) + 2, MEMF_CLEAR)) {
  181.  
  182.     dest[dindex++] = '"';    /* Start quote */
  183.  
  184.     src = rmptr->rm_Args[1];
  185.  
  186.     do {
  187.         ch = src[sindex++];
  188.  
  189.         if (ch == '"' || ch == qch ) {
  190.         dest[dindex++] = qch;
  191.         }
  192.         dest[dindex++] = ch;
  193.     } while (ch);
  194.  
  195.     dest[dindex-1] = '"';    /* End quote */
  196.  
  197.     (* retvalp) = CreateArgstring(dest, dindex);
  198.  
  199.     FreeMem(dest, (len<<1) + 2);
  200.  
  201.         /* ERR10_003 "Out of memory" */
  202.     return( (* retvalp) ? 0L : ERR10_003 );
  203.     }
  204.     return(ERR10_003);
  205. }
  206.